#include <cstdio>
#include <memory.h>

int min( int a, int b ) { return ( a < b )?a :b; }

int sum, n, m1, m2, sum1, sum2;
int a1[ 201 ], a2[ 201 ], c1[ 201 ], c2[ 201 ];
int f1[ 1000001 ], f2[ 1000001 ];

void init( )
{
    int i;
    double t;
    memset( f1, 0, sizeof( f1 ) );
    memset( f2, 0, sizeof( f2 ) );
    sum1 = sum2 = m1 = m2 = 0;
    while ( scanf("%lf", &t) )
    {
        if ( t < 0 )
            break;
        a1[ m1 ] = (int)( 100 * t + 0.5 );
        scanf("%dx", &c1[ m1 ]);
        sum1 += a1[ m1 ] * c1[ m1 ];
        m1++;
    }
    while ( scanf("%lf", &t) )
    {
        if ( t < 0 )
            break;
        a2[ m2 ] = (int)( 100 * t + 0.5 );
        scanf("%dx", &c2[ m2 ]);
        sum2 += a2[ m2 ] * c2[ m2 ];
        m2++;
    }
}

void work( )
{
    int i, j, k, ans = 1000000;
    for ( i = 0; i < m1; i++ )
    {
        if ( a1[ i ] == 0 ) continue;
        for ( j = sum1; j >= a1[ i ]; j-- )
            for ( k = 1; k <= c1[ i ] && j >= a1[ i ] * k; k++ )
                if ( j - a1[ i ] * k == 0 || f1[ j - a1[ i ] * k ] )
                    if ( !f1[ j ] )
                        f1[ j ] = f1[ j - a1[ i ] * k ] + k;
                    else
                        f1[ j ] = min( f1[ j ], f1[ j - a1[ i ] * k ] + k );
    }
    for ( i = 0; i < m2; i++ )
    {
        if ( a2[ i ] == 0 ) continue;
        for ( j = sum2; j >= a2[ i ]; j-- )
            for ( k = 1; k <= c2[ i ] && j >= a2[ i ] * k; k++ )
                if ( j - a2[ i ] * k == 0 || f2[ j - a2[ i ] * k ] )
                    if ( !f2[ j ] )
                        f2[ j ] = f2[ j - a2[ i ] * k ] + k;
                    else
                        f2[ j ] = min( f2[ j ], f2[ j - a2[ i ] * k ] + k );
    }
    for ( i = n; i <= sum1; i++ )
        if ( f1[ i ] && ( f2[ i - n ] || i - n == 0 ) )
            ans = min( ans, f1[ i ] + f2[ i - n ] );
    if ( ans == 1000000 )
        puts("The payment is impossible.");
    else
        printf("%d tenders must be exchanged.\n", ans);
}

int main( )
{
    double t;
    while ( scanf("%lf", &t ) )
    {
        if ( t < 0 )
            break;
        n = (int)( 100 * t + 0.5 );
        init( );
        work( );
    }
    return 0;
}
